﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lab1
{
    /// <summary>
    /// Interaction logic for QuestionFree.xaml
    /// </summary>
    public partial class QuestionFree : Page
    {
        QuestionService qService = new QuestionService();
        AnswerService aService = new AnswerService();
        public QuestionFree()
        {
            InitializeComponent();
            Question question = qService.getById((Guid)Application.Current.Properties["questionId"]);
            QuestionTitle.Content = question.Title;
            Title = question.Title;
            QuestionNumber.Text = UserAnswerService.CurrentNumber.ToString();
        }

        private void NextQuestion(object sender, RoutedEventArgs e)
        {
            
            Question question = qService.getById((Guid)Application.Current.Properties["questionId"]);
       
            
            IEnumerable<AnswerVariant> ans = aService.getByQuestion(question.Id);
            Guid deafultQuestionId = Guid.Empty;
            Guid nextQuestionId = Guid.Empty;
            Guid answerId = Guid.Empty;
            bool nextExist = false;

            foreach (AnswerVariant answer in ans)
            {
                
                if (Answer.Text.Equals(answer.Title))
                {
                    nextQuestionId = answer.QuestionChildId.GetValueOrDefault(Guid.Empty);
                    answerId = answer.Id;
                    nextExist = true;
                    break;
                }
                else
                {
                    deafultQuestionId = answer.QuestionChildId.GetValueOrDefault(Guid.Empty);
                }

            }
            UserAnswerService.Add(new UserAnswer
            {
                FreeAnswer = Answer.Text,
                QuestionId = question.Id,
                Number = UserAnswerService.CurrentNumber,
                AnswerVariantId= answerId
            });

            if (!nextExist)
            {
                nextQuestionId = deafultQuestionId;
            }

            if (nextQuestionId.Equals(Guid.Empty))
            {
                this.NavigationService.Navigate(new Result());
            }
            else
            {
                Application.Current.Properties["questionId"] = nextQuestionId;
                
                if (qService.getById(nextQuestionId).Type.Equals("Free"))
                {
                    this.NavigationService.Navigate(new QuestionFree());
                }
                else
                {
                    this.NavigationService.Navigate(new QuestionVariant());
                }
            }

        }

        private void PrevQuestion(object sender, RoutedEventArgs e)
        {
            UserAnswerService.Back();
            Question question = qService.getById((Guid)Application.Current.Properties["questionId"]);
            if (question.Type.Equals("Free"))
            {
                this.NavigationService.Navigate(new QuestionFree());
            }
            else
            {
                this.NavigationService.Navigate(new QuestionVariant());
            }
        }
    }
}
